import axios from 'axios'

export const BASEURL: string = 'http://127.0.0.1:8000/api'//'http://10.195.170.125:8000/api', // 实际运行时修改为服务器IP和端口

// 创建 axios 实例
const http = axios.create({

  baseURL: BASEURL,
  timeout: 1000000,
  headers: {
    'Content-Type': 'application/json',
  },
})

// 请求拦截器
http.interceptors.request.use(
  (config) => {
    // 可在此自动携带 token、id 等
    const token = localStorage.getItem('token')
    const id = localStorage.getItem('id')
    if (token) config.headers['token'] = token
    if (id) config.headers['id'] = id
    return config
  },
  (error) => Promise.reject(error),
)

// 响应拦截器
http.interceptors.response.use(
  (response) => response.data,
  (error) => Promise.reject(error),
)

export default http
